/*
Copyright (c) 2010 Vadim Seregin
Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
*/


#ifndef CARDTLIST_H
#define CARDTLIST_H

#include "wx/wxsqlite3.h"
#include "../../res/carddialog.h" // Base class: DictDialog
#include "baselist.h"
#include "cardwordlist.h"
#include "dblink.h"

class CardList : public CardDialog, public BaseList
{

public:
	CardList( DbLink* pcDbLink, wxString& soundFileDir );
	~CardList();

	// load dicts
	void Load();
	
protected:
	// get card name
	wxString GetCardName(){ return m_textCtrl->GetLineText(0); }
	
	// get primary language
	wxString GetPrimLang(){ return m_textCtrlPrimLang->GetLineText(0); }
	
	// get translation language
	wxString GetTranLang(){ return m_textCtrlTranLang->GetLineText(0); }
	
	// get learn check box
	wxString GetLearn(){ return m_checkBoxLearn->GetValue() ? wxT("yes") : wxT("no"); }
	wxString GetLearn( bool flag ){ return flag ? wxT("yes") : wxT("no"); }
	
	// set primary language
	void SetPrimLang( wxString& str ){ m_textCtrlPrimLang->SetValue( str ); }
	
	// set translation language
	void SetTranLang( wxString& str ){ m_textCtrlTranLang->SetValue( str ); }
	
	// set card name
	void SetCardName( wxString& str ){ m_textCtrl->SetValue( str ); }
	
	// set learn check box
	void SetLearn( wxString& str ){ bool bChecked = str == wxT("yes") ? 1 : 0; m_checkBoxLearn->SetValue( bChecked ); }
	void SetLearn( bool bChecked ){ m_checkBoxLearn->SetValue( bChecked ); }
	
	// fill list with dicts and return the number of dicts
	long FillCardList( bool bClear = false );
	
	// update item in the list
	void UpdateItem();
	
	// clear text in the TextCtrls
	void ClearText();
	
	// clear info
	void ClearInfo();
	
	wxString BoolToStr( wxString& checked ){ return checked == wxT("1") ? wxT("yes") : wxT("no"); }
	
	wxString StrToBool( wxString& str ){ return str == wxT("yes") ? wxT("1") : wxT("0"); }
	
	// update table names starting from iNum
	void TablesUpdate( long iNum );

	// ======================== events ================================

	// create or update dict
	virtual void OnOkClick( wxCommandEvent& event );
	
	// cancel button
	virtual void OnCancelClick( wxCommandEvent& event );
	
	// delete dict creation
	virtual void OnDeleteClick( wxCommandEvent& event );
	
	// open dict
	virtual void OnOpenClick( wxCommandEvent& event );
	
	// create dict
	virtual void OnCreateClick( wxCommandEvent& event );
	
	// if list item is selected
	virtual void OnCardSelected( wxListEvent& event );
	
	// if list item is deselected
	virtual void OnCardDeselected( wxListEvent& event );
	
	// left double click
	virtual void OnLeftDClick( wxMouseEvent& event ) { OnOpenClick( (wxCommandEvent&) event ); }
	
	// key down
	virtual void OnKeyDown( wxKeyEvent& event );

	// learn checkbox click
	virtual void OnLearnClick( wxMouseEvent& event );

	// enter window
	virtual void OnEnterWindow( wxMouseEvent& event );
	
protected:
	enum eCard
	{
		eCard_Name = 0,
		eCard_PrimLang,	
		eCard_TranLang,
		eCard_TableName,
		eCard_TotalWords,
		eCard_WordsLeft,
		eCard_Learn,
		eCard_Cols
	};
	
	wxSQLite3Database *m_pDictDb;   // pointer to dict database
	wxSQLite3Database *m_pCardDb;   // pointer to card database
	wxString m_sPrimLang;           // primary language
	wxString m_sTranLang;           // translation language
	wxString m_sCardName;           // card name
	wxString m_sTableName;          // table name
	wxString m_sLearn;              // learn checkbox
	long     m_iCardNum;            // number of dicts

	DbLink* m_pcDbLink;             // link to db
	
};

#endif // CARDTLIST_H
